package com.vehicle.controller;

import com.vehicle.bean.DataGridView;
import com.vehicle.bean.PageVo;
import com.vehicle.bean.ResultDto;
import com.vehicle.pojo.ApplyUse;
import com.vehicle.pojo.Car;
import com.vehicle.pojo.Check;
import com.vehicle.pojo.OutwardRecord;
import com.vehicle.service.ApplyUseService;
import com.vehicle.service.BackCarService;
import com.vehicle.service.CarExamineService;
import com.vehicle.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/backCar")
public class BackCarController extends DataTimeController{

    @Autowired
    private BackCarService backCarService;

    @Autowired
    private ApplyUseService applyUseService;

    @Autowired
    private CarExamineService carExamineService;

    @Autowired
    private CarService carService;

    //返回所有状态为出车中的申请单
    @RequestMapping("findAllApplyUse")
    @ResponseBody
    public DataGridView findAllApplyUse(PageVo pageVo,Integer deptid){
        DataGridView allApplyUse = backCarService.findAllApplyUse(pageVo,deptid);
        return allApplyUse;
    }

    //转发数据到回车登记界面
    @RequestMapping("backCarCheck")
    @ResponseBody
    public ModelAndView backCarCheck(Integer applyid){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/page/car/backCarCheck.jsp");
        ApplyUse applyUse = applyUseService.selectApplyUseById(applyid);
        modelAndView.addObject("applyUse",applyUse);
        Check check = carExamineService.selectCheckByApplyid(applyid);
        modelAndView.addObject("check",check);
        List<Car> carList = carExamineService.selectCarByLazy();
        modelAndView.addObject("carList",carList);
        return modelAndView;
    }

    //回车登记的操作
    @RequestMapping("backCarCheckAdd")
    @ResponseBody
    public ResultDto backCarCheckAdd(OutwardRecord outwardRecord,Integer applyid){
            ResultDto resultDto = new ResultDto();
            try {
                ApplyUse applyUse = applyUseService.selectApplyUseById(applyid);
                outwardRecord.setCarid(applyUse.getCar().getCarid());
                outwardRecord.setDeptid(applyUse.getDept().getDeptid());
                outwardRecord.setStarttime(applyUse.getUsestarttime());
                outwardRecord.setPlanendtime(applyUse.getUseendtime());
                outwardRecord.setSuite(applyUse.getCaruser());
                outwardRecord.setDriverid(applyUse.getCar().getDriver().getDriverid());
                outwardRecord.setDestination(applyUse.getBack());
                outwardRecord.setStartkm(applyUse.getCar().getCar_startkm());
                applyUseService.updateApplyUseState(applyid,5);
                carService.updateCarable(applyUse.getCar().getCarid(),2);
                Integer finalStartKM = outwardRecord.getEndkm() - applyUse.getCar().getCar_startkm()+ applyUse.getCar().getCar_startkm();
                carService.updateCarStartKM(applyUse.getCar().getCarid(),finalStartKM);
                backCarService.addOutWardRecord(outwardRecord);
                resultDto.setCode(1001);
            }catch (Exception e){
                e.printStackTrace();
                resultDto.setCode(-1001);
            }

            return resultDto;
    }


}
